<?php

namespace zhtfrontend\models\datas;

use Yii;
use common\models\datas\Customer;
use common\components\extenders\MenuManage;

/**
 * This is the model class for table "{{%pta_active}}".
 *
 * @property integer $id
 * @property integer $customer_id
 * @property string $title
 * @property string $content
 * @property integer $begin_time
 * @property integer $end_time
 * @property integer $site_id
 * @property integer $user_id
 * @property integer $created_at
 * @property integer $states
 */
class PtaActive extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%pta_active}}';
    }


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'content', 'customer_id', 'begin_time', 'end_time', 'created_at', 'states'], 'required'],
            [['id', 'customer_id', 'site_id', 'user_id', 'states'], 'integer'],
            [['content'], 'string'],
            [['title'], 'string', 'max' => 200]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', 'ID'),
            'customer_id' => Yii::t('zhtfrontend', '会员编号'),
            'title' => Yii::t('zhtfrontend', '活动标题'),
            'content' => Yii::t('zhtfrontend', '活动内容'),
            'begin_time' => Yii::t('zhtfrontend', '活动开始时间'),
            'end_time' => Yii::t('zhtfrontend', '活动结束时间'),
            'site_id' => Yii::t('zhtfrontend', '分店编号'),
            'user_id' => Yii::t('zhtfrontend', '活动创建人'),
            'created_at' => Yii::t('zhtfrontend', '加入时间'),
            'states' => Yii::t('zhtfrontend', '状态'),
        ];
    }

    public function extraFields()
    {
        return [
            'zhtfrontend_operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
        ];
    }


    public function fields()
    {
        $fields = parent::fields();
        $fields['states'] = function () {
            switch ($this->states) {
                case 0:
                    return \Yii::t('zhtfrontend', '未开始');
                case 1:
                    return \Yii::t('zhtfrontend', '进行中');
                case 2:
                    return \Yii::t('zhtfrontend', '已结束');
            }
        };
        $fields['begin_time'] = function () {
            return \Yii::$app->formatter->asDatetime($this->begin_time);
        };
        $fields['end_time'] = function () {
            return \Yii::$app->formatter->asDatetime($this->end_time);
        };
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        return $fields;
    }


    public function getCustomer()
    {
        return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
    }

}
